<template>
    <transition>
      <div class="at-dialog-warp" :style="changeStyle" v-show="visible" @click.self="handleWrapperClick">
        <div class="at-right-expand" :class="[{ 'at-dialog-expand': isExpand },expand]">
        <div
          :class="[sizeClass, customClass]"
          ref="dialog"
          :style="style">
          <div class="at-dialog__header at-dialog__header" >
            <!-- <slot name="title">
              <span class="at-dialog__title">{{title}}</span>
            </slot> -->
            <div v-if="$slots.headline" class="at_headline">
              <slot name="headline"></slot>
            </div>
            <div v-if="$slots.subhead" class="at_subhead">
              <slot name="subhead">
              </slot>
            </div>
        <!--     <div class="at-dialog__headerbtn">
              <i v-if="showClose" class="at-dialog__close at-icon at-icon-close" @click='handleClose'></i>
            </div> -->
            <img src="./images/展开1.png" class='at_expand' @click='handleExpand' v-show='!isExpand' v-if='sizeChange'>
            <img src="./images/展开4.png" class='at_expand' @click='handleExpandclose' v-show='isExpand' v-if='sizeChange'>
            <img src="./images/关闭.png" class='at_close' v-if="showClose" @click='handleClose'>

          </div>
          <div class="at-dialog__body" v-if="rendered" :class="subline"><slot></slot></div>
        </div>
        </div>
      </div>
  </div>
  </transition>
</template>
<script>
  import Popup from '../../common/utils/popup';
  import emitter from '../../common/mixins/emitter';
  export default {
    name: 'right-expand',
    data(){
      return{
        isExpand:false
      }
      
    },
    mixins: [Popup, emitter],

    props: {
      title: {
        type: String,
        default: ''
      },
      type:{
        type: String,
        default: 'normal'
      },
      modal: {
        type: Boolean,
        default: true
      },
      sizeChange:{
        type: Boolean,
        default: true
      },
      modalAppendToBody: {
        type: Boolean,
        default: true
      },

      lockScroll: {
        type: Boolean,
        default: true
      },

      closeOnClickModal: {
        type: Boolean,
        default: true
      },

      closeOnPressEscape: {
        type: Boolean,
        default: true
      },

      showClose: {
        type: Boolean,
        default: true
      },

      size: {
        type: String,
        default: 'small'
      },

      customClass: {
        type: String,
        default: ''
      },

      top: {
        type: String,
        default: '15%'
      },
      beforeClose: Function
    },

    watch: {
      visible(val) {
        this.$emit('update:visible', val);
        if (val) {
          document.body.style.overflow='hidden';
          this.$emit('open');
          this.$el.addEventListener('scroll', this.updatePopper);
          this.$nextTick(() => {
            this.$refs.dialog.scrollTop = 0;
          });
        } else {
          this.$el.removeEventListener('scroll', this.updatePopper);
          this.$emit('close');
          document.body.style.overflow='auto';
        }
      }
    },
    // mounted(){
    //      console.log(this.$slots.subhead)
    // },
    computed: {
      expand(){
        return `at-expand-${ this.type }`
      },
      sizeClass() {
        return `at-dialog--${ this.size }`;
      },
      subline(){
         if(this.$slots.subhead){
          return 'at-right-expand-sub'
         }
      },
      changeStyle(){
         return 'animation:'+`${ this.type }`+' 0.05s 1';
      },
      style() {
        return this.size === 'full' ? {} : { 'top': this.top };
      }
    },

    methods: {
      handleExpand(){
        this.isExpand=true;
      },
      handleExpandclose(){
        this.isExpand=false;
      },
      handleWrapperClick() {
        if (!this.closeOnClickModal) return;
        this.handleClose();
      },
      handleClose() {
        if (typeof this.beforeClose === 'function') {
          this.beforeClose(this.hide);
        } else {
          this.hide();
        }
        this.isExpand=false;
      },
      hide() {
        this.$emit('update:visible', false);
        this.$emit('visible-change', false);
      },
      updatePopper() {
        this.broadcast('ElSelectDropdown', 'updatePopper');
        this.broadcast('ElDropdownMenu', 'updatePopper');
      }
    },

    mounted() {
      if (this.visible) {
        this.rendered = true;
        this.open();
      }
    }
  };
</script>
